#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void StackInit(ST* p)
{
	assert(p);
	p->a = (STDataType*)malloc(4 * sizeof(STDataType));
	if (p->a == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	p->top = 0;
	p->capacity = 4;
}



void StackDestroy(ST* p)
{
	assert(p);
	free(p->a);
	p->a = NULL;
	p->top = p->capacity = 0;
}


void StackPush(ST* p, STDataType x)
{
	assert(p);
	if (p->top == p->capacity)
	{
		STDataType* tmp = (STDataType*)realloc(p->a, p->capacity * 2 * sizeof(STDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else {
			p->a = tmp;
			p->capacity *= 2;
		}
	}
	p->a[p->top++] = x;
}


void StackPop(ST* p)
{
	assert(p);
	assert(p->top > 0);
	p->top--;
}

STDataType StackTop(ST* p)
{
	assert(p);
	assert(p->top > 0);
	return p->a[p->top - 1];
}


int StackSize(ST* p)
{
	assert(p);
	return p->top;
}

bool StackEmpty(ST* p)
{
	assert(p);
	return p->top == NULL;
}